#!/bin/bash

<<comment
  此脚本为部署LVS脚本可供参考！！需自行配置yum 规划ip；内置两种模式：NAT模式  DR模式 
  环境自行准备如下：
  client1：eth0->192.168.88.10；网关192.168.88.5

  lvs1: eth0 -> 192.168.88.5；eth1->192.168.99.5

  web1：eth1->192.168.99.100；网关192.168.99.5

  web2：eth1->192.168.99.200；网关192.168.99.5
comment
read -p "1.NAT模式 2.DR模式  请选择：" u
echo -e "\033[5;36m===============================================\033[0m"
# 准备两台web服务器
cat > /tmp/md.txt <<EOF
192.168.88.5    lvs1
192.168.88.10   client1
192.168.99.100  web1
192.168.99.200  web2
EOF
md1=$(md5sum /tmp/md.txt | cut -d " " -f1)
md2=$(tail -4 /etc/hosts | md5sum | cut -d" " -f1)
[ "$md1" != "$md2" ] && cat >> /etc/hosts < /tmp/md.txt
rm -rf /tmp/md.txt
# 配置免密登陆
[ -f /root/.ssh/id_rsa.pub ] || ssh-keygen -t rsa -b 2048 -N '' -f /root/.ssh/id_rsa && chmod 0400 /root/.ssh/id_rsa
for host_name in lvs1 client1 web1 web2
do
ssh-copy-id -i /root/.ssh/id_rsa.pub $host_name 2> /dev/null
done
for nu in 1 2
do
ssh web$nu "yum -y install httpd;echo 'apache web server$nu' > /var/www/html/index.html;systemctl enable httpd --now" &> /dev/null
done
if test $u == 1;then
# 确保lvs1的ip转发功能打开
ip_forward=`tail -1 /etc/sysctl.conf | cut -d " " -f3`
[ "$ip_forward" != 1 ] && echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
sysctl -p &> /dev/null
yum install -y ipvsadm &> /dev/null  # 安装lvs
ipvsadm -A -t 192.168.88.5:80 -s rr # 为web服务器创建虚拟服务器，调度算法为rr
# 向虚拟服务器添加RIP
ipvsadm -a -t 192.168.88.5:80 -r 192.168.99.100 -w 1 -m
ipvsadm -a -t 192.168.88.5:80 -r 192.168.99.200 -w 2 -m
for i in {1..4}
do
curl http://lvs1
done
echo -e "\033[36mNAT模式设置成功！！\033[0m"
elif test $u == 2;then
yum -y install ipvsadm &> /dev/null
ipvsadm -D -t 192.168.88.5:80 &> /dev/null  # 清除lvs虚拟服务器配置
# 修改web1的eth0网卡配置文件
ssh web1 "cat > /etc/sysconfig/network-scripts/ifcfg-eth0 <<EOF
TYPE=Ethernet
BOOTPROTO=none
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=192.168.88.100
NETMASK=255.255.255.0
GATEWAY=192.168.88.254
EOF"
sleep 0.5
# 修改web2的eth0网卡配置文件
ssh web2 "cat > /etc/sysconfig/network-scripts/ifcfg-eth0 <<EOF
TYPE=Ethernet
BOOTPROTO=none
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=192.168.88.200
NETMASK=255.255.255.0
GATEWAY=192.168.88.254
EOF"
ssh -o ServerAliveInterval=5 web1 "ifdown eth0;ifup eth0;ifdown eth1"&> /dev/null  # 激活eth0网卡，关停eth1网卡  -- -o ServerAliveInterval=5 防止断网卡主ssh服务
ssh -o ServerAliveInterval=5 web2 "ifdown eth0;ifup eth0;ifdown eth1"&> /dev/null  # 激活eth0网卡，关停eth1网卡
ifdown eth1 &> /dev/null # 停掉lvs的eth1网卡
# 通过为eth0创建逻辑端口的方式配置vip，为逻辑端口起名为eth0:0,vip为192.168.88.15
cd /etc/sysconfig/network-scripts/
cat > ifcfg-eth0:0 <<EOF
TYPE=Ethernet
BOOTPROTO=none
NAME=eth0:0
DEVICE=eth0:0
ONBOOT=yes
IPADDR=192.168.88.15
PREFIX=24
EOF
ifup eth0:0
# 在两台web服务器的lo上配置vip 192.168.88.15
for i in 192.168.88.100 192.168.88.200
do
ssh $i "cat > /etc/sysconfig/network-scripts/ifcfg-lo:0 <<EOF
DEVICE=lo:0
IPADDR=192.168.88.15
NETMASK=255.255.255.255
NETWORK=192.168.88.15
BROADCAST=192.168.88.15
ONBOOT=yes
NAME=lo:0
EOF
ifup lo:0"
done
# 在两台web服务器上配置内核参数
for i in 192.168.88.100 192.168.88.200
do
ssh $i "cat >> /etc/sysctl.conf <<EOF
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2
EOF
sysctl -p" &> /dev/null
done
ipvsadm -A -t 192.168.88.15:80 -s wlc  # 创建虚拟服务器
# 向虚拟服务器中添加真实服务器
ipvsadm -a -t 192.168.88.15:80 -r 192.168.88.100 -w 1 -g
ipvsadm -a -t 192.168.88.15:80 -r 192.168.88.200 -w 2 -g
ssh client1 "for i in {1..6}; do curl -s http://192.168.88.15/; done"
echo -e "\033[36mDR模式开启成功！！\033[0m"
else
echo "请输入数字1或2"
fi

